package com.bci.pwtz.controller.app;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DeviceCountReq;
import com.bci.pwtz.common.remote.resp.DeviceCountResp;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.service.DeviceCountService;
@Controller
@RequestMapping("/api/count")
public class DeviceCountAppController extends AbstractController
{
@Autowired
DeviceCountService service;
@RequestMapping(value="/map",method=RequestMethod.POST)
@ResponseBody
public DeviceCountResp getDeviceCount(HttpServletRequest request){
    DeviceCountReq req=null;
    DeviceCountResp resp=new DeviceCountResp();
    try
    {
        req = (DeviceCountReq) getJsonToObj(request, DeviceCountReq.class);
        if (req == null)
        {
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("请求为空");
            return resp;
        }
        if(req.getTimestamp() == null || req.getCompanyId() == null){
            resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
            resp.setReturnMsg("参数错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getSecretKey() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("密钥错误");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (req.getUserId() == null)
        {
            resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
            resp.setReturnMsg("用户ID为空");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        if (StringUtils.isBlank(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
        {
            resp.setReturnCode(ReturnCode.TOKEN_INVALID);
            resp.setReturnMsg("票据无效");
            resp.setTimestamp(req.getTimestamp());
            return resp;
        }
        int count=-1;
        count= service.getDeviceRecordCount(req.getCompanyId());
        if (count != -1)
        {
            resp.setReturnCode(ReturnCode.SUCCESS);
            resp.setReturnMsg("查询数据成功");
            resp.setTimestamp(req.getTimestamp());
            resp.setCount(count);
        }else{
            resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
            resp.setReturnMsg("查询数据信息调用接口返回失败");
            resp.setTimestamp(req.getTimestamp());
        }
    }
    catch (PwtzDatabaseException e)
    {
        resp.setReturnCode(ReturnCode.DB_EXCEPTION);
        resp.setReturnMsg("数据库操作异常");
        resp.setTimestamp(req.getTimestamp());
    }
    catch (Exception e)
    {
        resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
        resp.setReturnMsg("系统错误");
        resp.setTimestamp(req.getTimestamp());
    }
    return resp;
}
}
